﻿namespace Extensions.Web.ViewModel
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Web.UI;

    public class MemberUtility
    {
        private static Dictionary<string, EventDescriptor> events = new Dictionary<string, EventDescriptor>();
        private static Dictionary<string, PropertyDescriptor> propertys = new Dictionary<string, PropertyDescriptor>();

        private static EventDescriptor FindEntryEventDescriptor(Type controlType, string eventName)
        {
            EventDescriptorCollection events = null;
            events = TypeDescriptor.GetEvents(controlType);
            foreach (EventDescriptor descriptor in events)
            {
                if (("On" + descriptor.Name).ToLower() == eventName.ToLower())
                {
                    if (!MemberUtility.events.ContainsKey(controlType.ToString() + "[" + descriptor.Name + "]"))
                    {
                        MemberUtility.events.Add(controlType.ToString() + "[" + descriptor.Name + "]", descriptor);
                    }
                    return descriptor;
                }
            }
            return null;
        }

        private static PropertyDescriptor FindEntryPropertyDescriptor(Type controlType, string propertyName)
        {
            PropertyDescriptorCollection properties = null;
            properties = TypeDescriptor.GetProperties(controlType);
            foreach (PropertyDescriptor descriptor in properties)
            {
                if (descriptor.Name.ToLower() == propertyName.ToLower())
                {
                    if (!propertys.ContainsKey(controlType.ToString() + "[" + descriptor.Name + "]"))
                    {
                        propertys.Add(controlType.ToString() + "[" + descriptor.Name + "]", descriptor);
                    }
                    return descriptor;
                }
            }
            return null;
        }

        internal static EventDescriptor GetEntryEventDescriptor(BoundPropertyEntry entry)
        {
            return FindEntryEventDescriptor(entry.ControlType, entry.Name);
        }

        public static EventDescriptor GetEntryEventDescriptor(Type controlType, string eventName)
        {
            if (events.ContainsKey(controlType.ToString() + "[" + eventName + "]"))
            {
                return events[controlType.ToString() + "[" + eventName + "]"];
            }
            return FindEntryEventDescriptor(controlType, "On" + eventName);
        }

        internal static PropertyDescriptor GetEntryPropertyDescriptor(BoundPropertyEntry entry)
        {
            return FindEntryPropertyDescriptor(entry.ControlType, entry.Name);
        }

        public static PropertyDescriptor GetEntryPropertyDescriptor(Type controlType, string propertyName)
        {
            if (propertys.ContainsKey(controlType.ToString() + "[" + propertyName + "]"))
            {
                return propertys[controlType.ToString() + "[" + propertyName + "]"];
            }
            return FindEntryPropertyDescriptor(controlType, propertyName);
        }
    }
}

